package com.zg.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;

@Slf4j
@RestControllerAdvice
public class RestExceptionHandler {


    /**
     * 默认全局异常
     */

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<String> exception(Exception e){
        log.error("全局异常 ex={}",e.getMessage(),e);
        if (e instanceof NoHandlerFoundException){
            return Result.fail(ResultCodeEnums.RC404.getCode(),e.getMessage());
        }
        return Result.fail(ResultCodeEnums.RC500.getCode(),e.getMessage());
    }

    @ExceptionHandler(BrokerException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result<String> brokerExcept(BrokerException e){
        return Result.fail(e.getCode(),e.getMessage());
    }

    /**
     * 处理请求参数格式错误 @RequestBody上使用@Valid 实体上使用@NotNull等，验证失败后抛出的异常是MethodArgumentNotValidException异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        //String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
        FieldError fieldError = e.getFieldError();
        return Result.fail(ResultCodeEnums.RC1005.getCode(),fieldError.getField().concat(fieldError.getDefaultMessage()));
    }

    /**
     * 处理Get请求中 使用@Valid 验证路径中请求实体校验失败后抛出的异常
     */
    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result BindExceptionHandler(BindException e) {
        //String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
        FieldError fieldError = e.getFieldError();
        return Result.fail(ResultCodeEnums.RC1005.getCode(),fieldError.getField().concat(fieldError.getDefaultMessage()));
    }

    /**
     * 处理请求参数格式错误 @RequestParam上validate失败后抛出的异常是ConstraintViolationException
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result ConstraintViolationExceptionHandler(ConstraintViolationException e) {
        String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining());
        return Result.fail(ResultCodeEnums.RC1005.getCode(),message);
    }

    /**
     * 参数格式异常
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result HttpMessageNotReadableExceptionHandler(HttpMessageNotReadableException e) {
        return Result.fail(ResultCodeEnums.RC1005.getCode(),e.getMessage());
    }
}
